//
//  WBHomeViewController.swift
//  赝品微博
//
//  Created by 刘浩林 on 2016/10/31.
//  Copyright © 2016年 刘浩林. All rights reserved.
//

import UIKit

//定义全局常量,尽量用private修饰,避免到处都可以访问
private let cellId = "cellId"


class WBHomeViewController: WBBaseViewController {

    //@objc lazy var statusList = [String]()
    //列表视图模型
     lazy var listViewModel = WBStatusListViewModel()

       //加载数据,重写父类方法
    override func loadData() {
        print("准备刷新最后一条\(self.listViewModel.statusList.last?.text)")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,shouldRefresh) in
            //刷新数据完成

            print("加载数据结束")

            //结束刷新控件
            self.refreshControl?.endRefreshing()

            //回复上拉刷新标记
            self.isPullup = false

            //刷新表格
            if shouldRefresh{
                 self.tableview?.reloadData()
            }

        }

//        //用网络工具加载数据AFNetworking
//
//        WBNetworkManager.shared.statusList { (list, isSuccess) in
//            //字典转模型,绑定表格数据
//            print(list)
//        }


//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = ["access_token":"2.00PiO2nDnL8VRC47b23486e6eiZCvC"]

//        WBNetworkManager.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
//            print(json)
//        }){ (_,  error) in
//            print("网络请求失败 \(error)")
//        }



//        WBNetworkManager.shared.request(URLString: urlString, parameters: params, completion: { (json,isSuccess) in
//            print(json)
//        })


        //print("开始加载数据 \(WBNetworkManager.shared)")
//
//        //模拟延时加载数据 -> dispatch_after
//        DispatchQueue.main.asyncAfter(deadline: .now() + 1){
//
//            for i in 0..<15 {
//                if self.isPullup{
//                    //将数据追加到底部
//                    self.statusList.append("上拉\(i)")
//                }else{
//                    //将数据插入顶部
//                    self.statusList.insert(i.description, at: 0)
//                }
//                self.statusList.insert(i.description, at: 0)
//            }
//            print("加载数据结束")
//
//            //结束刷新控件
//            self.refreshControl?.endRefreshing()
//
//            //回复上拉刷新标记
//            self.isPullup = false
//
//            //刷新表格
//            self.tableview?.reloadData()
//        }



    }
   @objc func showFriends() -> () {
        print(#function)
    let vc = WBDemoViewController()
    //vc.hidesBottomBarWhenPushed = true
    //push的动作时nav做的
    navigationController?.pushViewController(vc, animated: true)

    }

}

//MARK:-表格数据源方法,具体数据源方法实现,不需要super
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //1.取cell
        let cell = tableview?.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

        //2.设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text

        //3.返回cell
        return cell!
    }
}

//MARK:-设置界面
extension WBHomeViewController{
    override func setupTableview() {
        super.setupTableview()

        //设置导航栏按钮
        //无法高亮
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        //Swift 调用 OC 返回 Instancetype 的方法,判断不出是否可选
//        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)


        //navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIView)


        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))

        //注册原型 cell
        tableview?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)










            }
}
